
package com.candy.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.demoweb.model.dao.MemberDao;
import com.demoweb.model.dto.Member;

@WebServlet("/candy/candylogin.action")
public class CandyLoginServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		System.out.println("---------------로그인 서블릿 --------------------");
		
		// 1. 요청 데이터 읽기 (아이디, 비밀번호)
		String id = req.getParameter("memberId");
		String passwd = req.getParameter("passwd");
		
		// 2. 데이터 처리(db에서 조회)
		MemberDao dao = new MemberDao();
		Member member = dao.getMemberByIdAndPasswd(id,passwd);
		
		//System.out.println(member.get);
		
		// 3. member의 데이터 저장 하고 main으로 이동
		//( 로그인 성공 - member != NULL, 로그인 실패 - member == NULL )
		String path = null;
		
		if(member == null) { // 로그인 실패
			path = "";
		} else {
			HttpSession session = req.getSession();
			session.setAttribute("loginuser", member);
			
			if(req.getParameter("returnuri") != null) {
				path = req.getParameter("returnuri");
			} else {
				path = "/demoweb/candy/canbyMain.nhn";
			}
		} 

		resp.sendRedirect(path);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
}